package com.open.capacity.prints.controller;

import java.util.Map;

import com.open.capacity.annotation.log.LogAnnotation;
import com.open.capacity.commons.Result;
import com.open.capacity.prints.util.CronUtils;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.security.access.prepost.PreAuthorize;
import com.open.capacity.prints.entity.SpringScheduledCron;
import com.open.capacity.prints.service.SpringScheduledCronService;

import com.open.capacity.commons.PageResult;


/**
 * 定时任务表
 *
 * @author
 * @email
 * @date 2020-06-18 17:28:52
 */
@RestController
@RequestMapping("crons")
@Api(tags = "定时任务表")
public class SpringScheduledCronController {

    @Autowired
    private SpringScheduledCronService springScheduledCronService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @PreAuthorize("hasAnyAuthority('spring_scheduled_cron:springscheduledcron:list')")
    public PageResult list(@RequestParam Map<String, Object> params) {
        PageResult pageResult = springScheduledCronService.pageAll(params);
        return pageResult;
    }

    /**
     * 查询单条记录
     */
    @RequestMapping("/findById")
    @PreAuthorize("hasAnyAuthority('spring_scheduled_cron:springscheduledcron:list')")
    public Result findById(Integer id) {
        return Result.succeed(springScheduledCronService.findById(id));
    }

    /**
     * 保存
     */
    @RequestMapping("/saveOrUpdate")
    @PreAuthorize("hasAnyAuthority('spring_scheduled_cron:springscheduledcron:save')")
    @LogAnnotation(module = "定时任务", name = "更新")
    public Result save(@RequestBody SpringScheduledCron scheduledCron, Integer keyValue) {
        if (!CronUtils.isValidExpression(scheduledCron.getCronExpression())) {
            return Result.failed("失败,非法表达式:" + scheduledCron.getCronExpression());
        }
        scheduledCron.setCronId(keyValue);
        springScheduledCronService.update(scheduledCron);
        return Result.succeed("设置成功");
    }
}
